<template>
  <div class="grid-content bg-purple">
    <div class="table-action-wrapper" style="margin: 15px 0px 0px 10px">
      <el-button @click="beforeAddImage" style="margin-bottom: 20px"
        >添加图片</el-button
      >
      <el-button @click="previewDrawer = true" style="margin-bottom: 20px"
        >照片墙预览</el-button
      >
    </div>
    <div class="images-crads">
      <div class="image-wrapper" v-for="item in list" :key="item.id">
        <el-card :body-style="{ padding: '0px' }">
          <img :src="item.src" />
          <div style="padding: 10px 14px 5px">
            <div class="zh-name">{{ item.zh_name }}</div>
            <div class="bottom clearfix">
              <div class="en-name">{{ item.en_name }}</div>

              <el-popconfirm title="确定删除吗？" @onConfirm="handleDeletImage(item)">
                <el-button
                  slot="reference"
                  type="text"
                  style="float: right;color: orangered"
                  icon="el-icon-delete"
                ></el-button>
              </el-popconfirm>

              <el-button
                type="text"
                style="float: right"
                icon="el-icon-edit"
                @click="beforeUpdataImage(item)"
              ></el-button>
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <el-drawer title="图片信息" :visible.sync="drawer" direction="rtl">
      <el-form :model="imageObj" label-width="90px">
        <el-form-item label="图片">
          <el-upload
            class="avatar-uploader"
            name="f1"
            :action="action"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="imageObj.src"
              :src="host + '/' + imageObj.src"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="中文名称">
          <el-input style="width: 200px" v-model="imageObj.zh_name"> </el-input>
        </el-form-item>
        <el-form-item label="英文名称">
          <el-input style="width: 200px" v-model="imageObj.en_name"> </el-input>
        </el-form-item>
      </el-form>
      <div style="text-align: center">
        <el-button
          style="margin-left: 12px"
          type="success"
          @click="handleUpdateImage"
          v-if="imageObj.id"
          >更新</el-button
        >
        <el-button
          style="margin-left: 12px"
          type="primary"
          @click="handleAddImage"
          v-else
          >添加</el-button
        >
      </div>
    </el-drawer>

    <el-drawer
      title="照片墙预览"
      :visible.sync="previewDrawer"
      direction="rtl"
      size="1200px"
    >
    <div class="photo-wall-wapper" v-if="list.length > 0">
      <waterfall :list="list" :gutter="13" ref="waterfall">
        <template slot="item" slot-scope="props">
          <div class="water">
            <img
                :src="props.item.src"
                @load="$refs.waterfall.refresh()"
                style="width: 100%; height: 100%; object-fit: cover"
            />
          </div>
        </template>
      </waterfall>
    </div>
    </el-drawer>
  </div>
</template>

<script>
import { addImage, deleteImage, getImages, updateImage } from "@/api/picture";
import Waterfall from "vue-waterfall-plugin";

export default {
  components: { Waterfall },
  data() {
    return {
      list: [],
      listLoading: true,
      drawer: false,
      imageObj: {
        src: "",
        zh_name: "",
        en_name: "",
        position: 0,
      },
      host: process.env.VUE_APP_BASE_API,
      action: process.env.VUE_APP_BASE_API + "/api/v1/upPic",
      previewDrawer: false,
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getImages().then((res) => {
        this.list = res.data.lists;
        this.list.map(item => {
          item.src = this.host + "/" + item.src
        })
        this.listLoading = false;
      });
    },

    handleAvatarSuccess(res) {
      this.imageObj.src = res.raw;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type.indexOf("image") > -1;
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传图片只能是 image 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    beforeAddImage() {
      if (this.imageObj.id) {
        this.imageObj = {
          src: "",
          zh_name: "",
          en_name: "",
          position: 0,
        };
      }
      this.drawer = true;
    },
    beforeUpdataImage(obj) {
      this.imageObj = JSON.parse(JSON.stringify(obj));
      this.drawer = true;
    },

    handleAddImage() {
      let tmp = JSON.parse(JSON.stringify(this.imageObj));
      tmp.position = tmp.position + "";
      addImage(tmp).then((res) => {
        this.drawer = false;
        this.fetchData();
      });
    },

    handleUpdateImage() {
      let tmp = JSON.parse(JSON.stringify(this.imageObj));
      let id = tmp.id;
      delete tmp.id;

      updateImage(id, JSON.stringify(tmp)).then((res) => {
        this.drawer = false;
        this.fetchData();
      });
    },

    handleDeletImage(obj) {
      deleteImage(obj.id).then((res) => {
        if (res.code && res.code == 200) {
          this.fetchData();
        }
      });
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 200px;
  height: 200px;
  line-height: 200px;
  text-align: center;
}
.avatar {
  max-width: 200px;
  max-height: 200px;
  display: block;
  object-fit: cover;
}
.images-crads {
  display: flex;
  flex-wrap: wrap;
  /* justify-content: space-evenly; */
}
.image-wrapper {
  width: 200px;
  margin: 0px 10px 15px;
}

.image-wrapper img {
  width: 100%;
  height: 150px;
  object-fit: cover;
}

.zh-name,
.en-name {
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.en-name {
  display: inline-block;
  width: 100px;
  color: gray;
  font-size: 12px;
}
.bottom {
  margin-top: 8px;
  line-height: 12px;
}
.bottom .el-button {
  padding: 0 2px;
}

.el-table th.gutter {
  /* 
    * 解决element-ui 表格篡位的问题 👇 
    */
  display: table-cell !important;
}
</style>
